import matplotlib.pyplot as plt
import pandas as pd
import numpy as np


# 1. 创建Series
s1 = pd.Series([10,20,30,40], index=["a","b","c","d"])

# 2. 创建DataFrame
data = {
    "姓名": ["张三","李四","王五","赵六","钱七"],
    "年龄": [18,19,18,20,19],
    "语文成绩": [85,92,78,90,88],
    "数学成绩": [90,85,82,95,86]
}
df = pd.DataFrame(data)

# 数据准备
students = df["姓名"]
math_scores = df["数学成绩"]

# 中文字体设置（避免乱码）
plt.rcParams['font.sans-serif'] = ['SimHei', 'DejaVu Sans']
plt.rcParams['axes.unicode_minus'] = False


plt.figure(figsize=(10,6))
# 直方图
n, bins, patches = plt.hist(df["语文成绩"], bins=5, edgecolor='black', color='#9467bd', alpha=0.7)
# 柱子顶部添加人数标签
for patch in patches:
    height = patch.get_height()
    plt.text(patch.get_x() + patch.get_width()/2., height + 0.1,
             f'{int(height)}', ha='center', va='bottom')
plt.title('语文成绩分布直方图', fontsize=14)
plt.xlabel('成绩区间（分）')
plt.ylabel('学生人数（人）')
plt.grid(True, alpha=0.3, axis='y')
plt.show()